<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Tag;
use App\Constants\MiniProgramConstant;
use App\Constants\TagConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;

class ArticleTagController extends AdminController
{
    protected $title = '新闻咨询标签';

    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->addLink('新闻咨询', admin_route('news_articles.index'));
        $tab->add('标签', $this->grid(), true);
        $tab->addLink('草稿箱', admin_route('news_articles_draft.index'));
        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @param string $type
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Tag(), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->model()->orderByDesc('id');
            $grid->model()->where('type', '=', TagConstant::TAG_BY_ARTICLE);

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('name');
            $grid->column('status')->using(['否', '是'])->label(['danger', 'primary']);
            $grid->column('sort');
            $grid->column('created_at');

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableViewButton();
            $grid->disableRowSelector();

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->like('name')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Tag(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Tag(), function (Form $form) {
            $form->display('id');
            $form->hidden('type')->value(TagConstant::TAG_BY_ARTICLE);
            $form->select('mini_type')->options(function () {
                return getAuthMiniProgramMap(Admin::user()->id);
            })->required();
            $form->text('name')->required();
            $form->radio('status')->options(['否', '是'])->default(0);
            $form->number('sort');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
